<script>
// import { getNewVersion } from '@/api/common.js';
export default {
	onLaunch: function () {
		console.log('APP onLaunch');
		
		uni.removeStorageSync('isShowModal');
		this.setUserClientId();
		this.setVersion();
		this.getVersion();
		// 第一次打开进入到登录页面
		// TODO  解决退出登录后 杀死程序 再打开 多次跳转login
		const userInfo = uni.getStorageSync('loginUserInfo') || {};
		if (!Object.keys(userInfo).length) {
			uni.setStorageSync('onLaunch', 1);
			uni.reLaunch({
				url: '/pages/login/login',
			});
		}
	},
	onShow: function () {
		console.log('APP onShow');
		// uni.removeStorageSync('isShowModal');
		
		// this.getAuth();
		
	},
	onHide: function () {
		console.log('APP onHide');
	},
	methods: {
		doUpData(url,types) {
			if(url){
				if(types == '1'){
					plus.runtime.openURL(url)
				}else{
					uni.showLoading({
						title: '更新中……',
					});
					uni.downloadFile({
						//执行下载
						url: url, //下载地址
						success: (downloadResult) => {
							//下载成功
							uni.hideLoading();
							if (downloadResult.statusCode == 200) {
								plus.runtime.install(
									//安装
									downloadResult.tempFilePath,
									{
										force: true,
									},
									function (res) {
										utils.showToast('更新成功，重启中');
										plus.runtime.restart();
									}
								);
							}
						},
					}); 
				}
			}
		},
		// 存储客户端id
		setUserClientId() {
			if (uni.getSystemInfoSync().platform === 'android') {
				
			}else {
				return;
			}
			// 获取手机ID [unipush 推送]
			var cid = plus.push.getClientInfo().clientid;
			uni.setStorageSync('user_cid', cid);
			plus.push.addEventListener(
				'click',
				function (msg) {
					console.log('click:' + JSON.stringify(msg));
					//这里可以写跳转业务代码
				},
				false
			);
			// 监听在线消息事件
			plus.push.addEventListener(
				'receive',
				function (msg) {
					// plus.ui.alert(2);
					//这里可以写跳转业务代码
					console.log('recevice:' + JSON.stringify(msg));
				},
				false
			);
			//用户判断每次的页面显示是否需要刷新
			uni.setStorageSync('refreshData', false);
		},
		setVersion(){
			uni.setStorageSync("app_version",plus.runtime.version);
		},
		// 获取是否有版本更新
		async getVersion() {
			console.log('App.vue getVersion');
			
			const token = uni.getStorageSync('Admin-Token') || {};
			// 只有登录的状态下，才会请求版本信息
			if (!token.length) {
				return;
			}
			var _this = this;
			let data = { appType: '' };
			if (uni.getSystemInfoSync().platform === 'android') {
				data.appType = '0';
			} else {
				data.appType = '1';
			}
			let res = await this.$api.common.getNewVersion(data);
			if (res.code == 200 && res.mobile.version && res.mobile.version != plus.runtime.version) {
				uni.showModal({
					title: '发现新版本',
					content: '本次更新内容:\n' + res.mobile.updateDescription,
					showCancel: res.mobile.forciblyUpdate == '0',
					success: (rs) => {
						if (rs.confirm == true) {
							//当用户确定更新，执行更新
							_this.doUpData(res.mobile.downloadUrl,data.appType);
						}
					},
				});
			}
		},
	},
};
</script>
<style lang="scss">
@import 'uview-ui/index.scss';
/*每个页面公共css */
@import './common/common.scss';
</style>